﻿$(document).ready(function() {
    $('#frmRequest-content').submit(function () {
        if (confirm("Are you sure you want to decline request?")) {
            App.blockUI($("#ListRequest-content"), false);
            $.ajax({
                method: "POST",
                url: "/OutboundCentre/DeleteList",
                data: $(this).serialize(),
                success: function(data) {
                    $('#frFilterOutboundCentre').submit();
                    App.unblockUI($("#ListRequest-content"), false);
                },
                error: function() {
                    $("#msg-ListRequest-content").html("Action failed");
                    App.unblockUI($("#ListRequest-content"), false);
                }
            });
        }
        return false;
    });
});
function ViewDetail(id) {
    App.blockUI($("#ListRequest-content"), false);
    $.ajax({
        method: "GET",
        url: "/OutboundCentre/ViewDetail",
        data: { id: id },
        success: function (data) {
            $("#ViewDetail").html(data);
            $("#ViewDetail").modal();
            App.unblockUI($("#ListRequest-content"), false);
        },
        error: function () {
            $("#ViewDetail").html("Load failed");
            $("#ViewDetail").modal();
            App.unblockUI($("#ListRequest-content"), false);
        }
    });
}
function ViewOutboundApproveModal(id,actionId) {
    App.blockUI($("#ListRequest-content"), false);
    $.ajax({
        method: "Get",
        url: "/OutboundCentre/Approve",
        data: { id: id , actionId:actionId},
        success: function (data) {
            $("#ApproveModal").html(data);
            $("#ApproveModal").modal();
            App.unblockUI($("#ListRequest-content"), false);
            initOutboundApproveModel();
        },
        error: function () {
            $("#ApproveModal").html("Load failed");
            $("#ApproveModal").modal();
            App.unblockUI($("#ListRequest-content"), false);
        }
    });
}

var initOutboundApproveModel = function () {
    $('#frOutboundApprove').submit(function () {
        App.blockUI($("#frOutboundApprove"), false);
        $.ajax({
                method: "POST",
                url: $('#frOutboundApprove').attr("action"),
                data: $(this).serialize(),
                success: function (data) {
                    $('#frFilterOutboundCentre').submit();
                    App.unblockUI($("#frOutboundApprove"), false);
                    $("#ApproveModal").modal('hide');
                    //$("#msg-ListRequest-content").html(data);
                },
                error: function () {
                    $("#msg-ListRequest-content").html("Action failed");
                    App.unblockUI($("#frOutboundApprove"), false);
                }
            });
        return false;
    });
};
function CompleteRequest(id, action,email) {
    App.blockUI($("#ListRequest-content"), false);
    //No Answer – Attempt 2 à Prompt agent to email customer, once agent clicked ok please remove from list
    if (action == 2) {
        if (!confirm("Please email to customer")) {
            return;
        } else {
            var openWindow = window.open("mailto:" + email, 'emailWindow');
            if (openWindow && window.open && !window.closed)
                openWindow.close();
        }
    }
    //Order not Taken à create pop up for text for reason why sale not take, once entered remove from list
    if (action == 11) {
        ViewOutboundApproveModal(id, action);
        initOutboundApproveModel();
        return;
    }
    if (action == 0) {
        alert('Please select action !');
    } else {
        $.ajax({
            method: "POST",
            url: '/OutboundCentre/Complete',
            data: { id: id, actionId: action },
            success: function(data) {
                $('#frFilterOutboundCentre').submit();
                App.unblockUI($("#ListRequest-content"), false);
            },
            error: function() {
                $("#msg-ListRequest-content").html("Action failed");
                App.unblockUI($("#ListRequest-content"), false);
            }
        });
    }
} 

function DeleteRequest(id) {
    App.blockUI($("#ListRequest-content"), false);
    if (confirm("Are you sure you want to decline this request?")) {
        $.ajax({
            method: "POST",
            url: '/OutboundCentre/Delete',
            data: { id: id },
            success: function(data) {
                $('#frFilterOutboundCentre').submit();
                App.unblockUI($("#ListRequest-content"), false);
            },
            error: function() {
                $("#msg-ListRequest-content").html("Action failed");
                App.unblockUI($("#ListRequest-content"), false);
            }
        });
    }
}